package com.lzh.financial.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzh.financial.bean.PPayTypeBean;
import com.lzh.financial.bean.PUserBean;
import com.lzh.financial.bean.PUserMoneyBean;
import com.lzh.financial.bean.PpayBean;
import com.lzh.financial.params.PageParam;
import com.lzh.financial.params.PageVo;
import com.lzh.financial.service.IPayService;
import com.lzh.financial.service.IPayTypeService;
import com.lzh.financial.service.IUserService;
import com.lzh.financial.service.IuserMoneyService;
import com.lzh.financial.util.JwtTokenUtil;
import com.lzh.financial.util.RebornJson;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Resource
    private IuserMoneyService userMoneyService;
    @Resource
    private IUserService userService;
    @Resource
    private IPayService payService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Autowired
    private IPayTypeService payTypeService;

    /**
     * 设置交易类型
     * @return
     */
    @RequestMapping(value = "/setPayType",method = RequestMethod.POST)
    public JSONObject setPayType(PPayTypeBean payType){
        if (StringUtils.isBlank(payType.getName())) {
            return RebornJson.fail("名字不能为空");
        }
        Date date = new Date();
        PPayTypeBean typeBean = payTypeService.getById(payType.getId());
        payType.setCreateTime(date);
        payType.setUpdateTime(date);
        if (ObjectUtil.isNull(typeBean)) {
            payTypeService.save(payType);
        }else {
            payType.setId(typeBean.getId());
            payTypeService.updateById(payType);
        }
        return RebornJson.success("设置成功");
    }

    /**
     * 添加交易记录
     * @return
     */
    @RequestMapping(value = "/addPay",method = RequestMethod.POST)
    public JSONObject addPay(PpayBean payBean,HttpSession session){
        String id = jwtTokenUtil.getUserIdFromToken(session);
        PUserBean userBean = userService.getById(id);
        if (userBean.getRole()!=3){
            return RebornJson.fail("没有权限");
        }
        if (ObjectUtil.isNull(payBean.getPayUserId())) {
            payBean.setPayUserId(Integer.valueOf(id));
        }if (ObjectUtil.isNull(payBean.getUserId())) {
            return RebornJson.fail("用户不能为空");
        } if (ObjectUtil.isNull(payBean.getWay())) {
            return RebornJson.fail("请选择资金流动方式");
        }  if (ObjectUtil.isNull(payBean.getType())) {
            return RebornJson.fail("类型不能为空");
        }   if (StringUtils.isBlank(payBean.getProfile())) {
            return RebornJson.fail("备注不能为空");
        }  if (ObjectUtil.isNull(payBean.getPayMoney())||payBean.getPayMoney().intValue()==0) {
            return RebornJson.fail("金额不能为空");
        }
        PUserMoneyBean bean = userMoneyService.getOne(new QueryWrapper<PUserMoneyBean>().eq("user_id",payBean.getUserId()));
        Date date=new Date();
        if (ObjectUtil.isNull(bean)) {
            bean= new PUserMoneyBean(payBean.getUserId(),BigDecimal.valueOf(0));
            bean.setCreateTime(date);
            bean.setUpdateTime(date);
            userMoneyService.save(bean);
        }
        payBean.setPyear(DateUtil.date().year());
        payBean.setPmonth(DateUtil.date().month()+1);
        payBean.setPday(DateUtil.date().dayOfMonth());
        payBean.setCreateTime(date);
        payBean.setUpdateTime(date);
        BigDecimal bigDecimal;
        if (payBean.getWay() == 2) {
            bigDecimal= new BigDecimal(bean.getTotalMoney().floatValue() + payBean.getPayMoney().floatValue());
            bean.setTotalMoney(bigDecimal) ;
            userMoneyService.updateById(bean);
        }else {
            bigDecimal= new BigDecimal(bean.getTotalMoney().floatValue() - payBean.getPayMoney().floatValue());
            bean.setTotalMoney(bigDecimal);
            userMoneyService.updateById(bean);
        }
        payBean.setTotalMoney(bigDecimal);
        if (!payService.save(payBean)) {
            return RebornJson.fail("保存失败");
        }
        return RebornJson.success("设置成功");
    }

//    删除-------------------

    /**
     * 删除交易类型
     * @return
     */
    @RequestMapping(value = "/delPayType",method = RequestMethod.POST)
    public JSONObject delPayType(Integer id){
        return payTypeService.removeById(id)?RebornJson.success("删除成功"):RebornJson.fail("删除失败");
    }



//    查询------------
    /**
     * 查询交易类型
     * @return
     */
    @RequestMapping(value = "/getPayType",method = RequestMethod.GET)
    public JSONObject getPayType(){
        return RebornJson.success("查询成功",payTypeService.list());
    }



}
